package email;

import java.io.*;
import java.sql.*;

public class TestCaseDoc {
	
	public static void main(String[] args) throws Exception {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (ClassNotFoundException e) {
			System.out.println("Oracle JDBC Driver not found!");
			e.printStackTrace();
			return;
		}
		Connection connection = null;
		try {
			connection = DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:xe", "mapadmin",
					"mapadmin");
		} catch (SQLException e) {
			System.out.println("Connection Failed!");
			e.printStackTrace();
			return;
		}
		
		if (connection != null) {
		    String sql = "SELECT * FROM CASEDOC WHERE CD_ID = '1'";
		    PreparedStatement stmt = connection.prepareStatement(sql);
		    ResultSet rs = stmt.executeQuery();
		    System.out.println("File download started...");
		    while (rs.next()) {
		      String name = rs.getString("CD_NAME");
		      System.out.println(name);
		      File image = new File("C:\\Users\\ASB\\Desktop\\" + name);
		      FileOutputStream fos = new FileOutputStream(image);
	
		      byte[] buffer = new byte[1];
		      InputStream is = rs.getBinaryStream("CD_DATA");
		      while (is.read(buffer) > 0) {
		        fos.write(buffer);
		      }
		      fos.close();
		    }
		    System.out.println("File downloaded successfully!");
		    rs.close();
			stmt.close();
			connection.close();
		}
	  }
}
